/**
 * There are code generation
 */
package com.neusoft.lohas.modules.ivm.web.product;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.neusoft.lohas.common.config.Global;
import com.neusoft.lohas.common.persistence.Page;
import com.neusoft.lohas.common.utils.StringUtils;
import com.neusoft.lohas.common.web.BaseController;
import com.neusoft.lohas.modules.ivm.entity.product.ProductUnit;
import com.neusoft.lohas.modules.ivm.service.product.ProductUnitService;
import com.neusoft.lohas.modules.sys.entity.User;
import com.neusoft.lohas.modules.sys.utils.UserUtils;

/**
 * 商品单位Controller
 * @author Jackie.Liu
 * @version 2014-10-30
 */
@Controller
@RequestMapping(value = "${adminPath}/ivm/product/unit")
public class ProductUnitController extends BaseController {

	@Autowired
	private ProductUnitService productUnitService;
	
	@ModelAttribute
	public ProductUnit get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return productUnitService.get(id);
		}else{
			return new ProductUnit();
		}
	}
	
	@RequiresPermissions("ivm:product:unit:view")
	@RequestMapping(value = {"list", ""})
	public String list(ProductUnit productUnit, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			productUnit.setCreateBy(user);
		}
		productUnit.setMerchant(super.merchant);
        Page<ProductUnit> page = productUnitService.find(new Page<ProductUnit>(request, response), productUnit); 
        model.addAttribute("page", page);
        if(productUnit.getType().equals("1")){//销售单位
        	return "modules/ivm/set/productUnitList";
        }
        //采购单位
		return "modules/ivm/set/productPurchaseUnitList";
	}

	@RequiresPermissions("ivm:product:unit:view")
	@RequestMapping(value = "form")
	public String form(ProductUnit productUnit, Model model) {
		if (StringUtils.isBlank(productUnit.getName())) {
			productUnit.setMerchant(super.merchant);
		}
		model.addAttribute("productUnit", productUnit);
		return "modules/ivm/set/productUnitForm";
	}

	@RequiresPermissions("ivm:product:unit:edit")
	@RequestMapping(value = "save")
	public String save(ProductUnit productUnit, String oldName, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, productUnit)){
			return form(productUnit, model);
		}
		if (!"true".equals(checkName(productUnit.getMerchant().getId(),productUnit.getType(),productUnit.getName(),oldName))){
			addMessage(model, "保存商品单位'" + productUnit.getName() + "'失败，商品单位已存在");
			return form(productUnit, model);
		}
		productUnitService.save(productUnit);
		addMessage(redirectAttributes, "保存商品单位'" + productUnit.getName() + "'成功");
		return "redirect:"+Global.getAdminPath()+"/ivm/product/unit/?repage&type="+productUnit.getType();
	}
	
	@RequiresPermissions("ivm:product:unit:edit")
	@RequestMapping(value = "delete")
	public String delete(String id,String type, RedirectAttributes redirectAttributes) {
		productUnitService.delete(id);
		addMessage(redirectAttributes, "删除商品单位成功");
		return "redirect:"+Global.getAdminPath()+"/ivm/product/unit/?repage&type="+type;
	}

	@ResponseBody
	@RequiresPermissions("ivm:product:unit:edit")
	@RequestMapping(value = "checkName")
	public String checkName(String mId, String type, String name, String oldName) {
		if (name !=null && name.equals(oldName)) {
			return "true";
		} else if (name !=null && productUnitService.findByName(type, name,mId) == null) {
			return "true";
		}
		return "false";
	}
	
	/**
	 * 批量修改单位排序
	 */
	@RequiresPermissions("ivm:product:unit:edit")
	@RequestMapping(value = "updateSort")
	public String updateSort(String[] ids, Integer[] sorts, String type, RedirectAttributes redirectAttributes) {
		if(Global.isDemoMode()){
			addMessage(redirectAttributes, "演示模式，不允许操作！");
			return "redirect:"+Global.getAdminPath()+"/ivm/product/unit/?repage&type="+type;
		}
    	int len = ids.length;
    	ProductUnit[] units = new ProductUnit[len];
    	for (int i = 0; i < len; i++) {
    		units[i] = productUnitService.get(ids[i]);
    		units[i].setSort(sorts[i]);
    		productUnitService.save(units[i]);
    	}
    	addMessage(redirectAttributes, "保存单位排序成功!");
		return "redirect:"+Global.getAdminPath()+"/ivm/product/unit/?repage&type="+type;
	}
	
	/**
	 * 批量修改单位排序-手机端滑动排序异步保存
	 */
	@RequiresPermissions("ivm:product:unit:edit")
	@RequestMapping(value = "touch/updateSort")
	public @ResponseBody Object touchUpdateSort(String[] ids, Integer[] sorts, RedirectAttributes redirectAttributes) {
    	int len = ids.length;
    	ProductUnit[] units = new ProductUnit[len];
    	for (int i = 0; i < len; i++) {
    		units[i] = productUnitService.get(ids[i]);
    		units[i].setSort(sorts[i]);
    		productUnitService.save(units[i]);
    	}
		return getStatusBean(200, "成功");
	}

}
